package ext.tianma.changeManager.service;

import java.io.IOException;
import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.remoting.support.RemoteAccessor;
import org.springframework.stereotype.Service;

import wt.access.NotAuthorizedException;
import wt.change2.ChangeHelper2;
import wt.change2.ChangeNoticeComplexity;
import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeOrder2;
import wt.doc.WTDocument;
import wt.doc.WTDocumentMaster;
import wt.fc.ObjectReference;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTArrayList;
import wt.fc.collections.WTHashSet;
import wt.fc.collections.WTList;
import wt.folder.Folder;
import wt.folder.FolderEntry;
import wt.folder.FolderHelper;
import wt.iba.definition.litedefinition.AttributeDefDefaultView;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerException;
import wt.lifecycle.LifeCycleState;
import wt.lifecycle.LifeCycleTemplate;
import wt.lifecycle.State;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.ownership.Ownership;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.query.QuerySpec;
import wt.type.TypeDefinitionReference;
import wt.type.TypedUtility;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.vc.VersionControlHelper;
import wt.vc.wip.WorkInProgressHelper;

import com.ptc.core.lwc.common.view.AttributeDefinitionReadView;
import com.ptc.core.lwc.common.view.PropertyValueReadView;
import com.ptc.core.lwc.common.view.TypeDefinitionReadView;
import com.ptc.core.lwc.server.TypeDefinitionServiceHelper;

import ext.com.core.CoreUtil;
import ext.com.iba.IBAUtil;
import ext.com.workflow.WorkflowUtil;
import ext.generic.erp.common.CommonPDMUtil;
import ext.tianma.bom.service.MaterialReverseService;
import ext.tianma.bom.service.MaterialsApplicationService;
import ext.tianma.bom.util.DBUtil;
import ext.tianma.bom.util.GenericUtil;
import ext.tianma.bom.util.LWCUtil;
import ext.tianma.changeManager.config.ChangeConfig;
import ext.tianma.changeManager.constants.ChangeConstants;
import ext.tianma.changeManager.util.ChangeUtil;
import ext.tianma.changeManager.util.ProcessUtil;
import ext.tianma.changeManager.util.QueryUtil;
import ext.tianma.changeManager.vo.ChangeVO;
import ext.tianma.model.RDPBOMModel;
import ext.tianma.model.RDPChange;
import ext.tianma.model.TMECOITEMModel;
import ext.tianma.model.util.TMObjectUtilHelper;
import ext.tianma.number.util.DocUtil;
import ext.tianma.number.util.PartUtil;
import ext.tianma.project2.util.ProjectUtil;
import ext.tianma.util.FolderUtil;
import ext.tianma.util.TMUtil;
import ext.tianma.util.UserUtil;

@Service
public class ChangeService extends RemoteAccessor{
	
	private static final String OBSOLESCENCE = "OBSOLESCENCE"; //废弃状态
	//流程运行状况：open：rpd正在提交表单， closed：windchill已完成任务，FAILED：失败
	public static final String OPEN = "open";
	public static final String CLOSED = "closed";
	public static final String FAILED = "failed";
	
	private static final String RAWMATERIAL = "rawMaterial";
	private static final String RELEASED = "RELEASED";//状态：已发布
	private static final String OBJOID = "objOid";//受影响对象oid标识
	
	private static final String PARTNUMBEREFFECTIVESITE = "料号生效产地";
	
	//物料进阶、图文档修订的流程变量
	public static final String OLDPART = "oldPart";
	public static final String OLDDOC = "oldDoc";
	
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
	
	/**
	 * 查询文档
	 * @param docNumber
	 * @param docName
	 * @return
	 * @author xucr
	 * @CreateDate 2020年11月25日
	 */
	public JSONObject findWTDocyment(String data) {
		JSONObject result = null;
		try {
			JSONObject dataObj = new JSONObject(data);
			String docNumber = ChangeUtil.getJSONString(dataObj, "docNumber");
			String docName = ChangeUtil.getJSONString(dataObj, "docName");
			String docType = ChangeUtil.getJSONString(dataObj, "docType"); // 1,工艺文件，2 ：图文档
			String typeTip = "";
			if(StringUtils.isEmpty(docNumber) && StringUtils.isEmpty(docName)) {
				result = ChangeUtil.resFailed("文档编码和名称不能同时为空");
			} else {
				//取消文档类型控制,界面已经整合在一起了
//				if("1".equals(docType)) {
//					docType = ChangeConstants.DOCTYPES[0];
//					typeTip = "工艺文件";
//				} else {
//					docType = ChangeConstants.DOCTYPES[1];
//					typeTip = "图档类文档";
//				}
				JSONObject obj = new JSONObject();
				obj.put("docNumber", docNumber);
				obj.put("docName", docName);
				obj.put("softType", docType);
				obj.put("states", RELEASED);
				QuerySpec qs = QueryUtil.queryWTDocument(obj);
				logger.info("findWTDocyment sql>>>" + qs.toString());
				QueryResult qr = PersistenceHelper.manager.find(qs);
				List<WTDocumentMaster> masters = new ArrayList<WTDocumentMaster>();
				if(qr.size() > 0) {
					JSONArray array = new JSONArray();
					JSONObject docInfo = null;
					while(qr.hasMoreElements()) {
						Object [] o = (Object[]) qr.nextElement();
						WTDocument doc = (WTDocument) o[0];
						WTDocumentMaster master = (WTDocumentMaster) doc.getMasterReference().getObject();
						if(!masters.contains(master)) {
							masters.add(master);
							doc= DocUtil.getLatestWTDocument(master);
							if(RELEASED.equals(doc.getState().getState().toString())) {
								docInfo = docInfo(doc, null);
								array.put(docInfo);
							}
						}
					}
					result = ChangeUtil.resSuccess(array, array.length());
				} else {
					result = ChangeUtil.resFailed("编码为【" + docNumber + "】的"+typeTip+"在PLM系统中未发布或不存在");
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (RemoteException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 通过料号查询工艺文件、图档类文档
	 * @param data
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月23日
	 */
	public JSONObject findWTDocumentByPart(String data) {
		JSONObject result = null;
		/**
		 * 查询所有视图下的工艺文件、图档类文档
		 * */
		try {
			JSONObject dataObj = new JSONObject(data);
			String partNumber = ChangeUtil.getJSONString(dataObj, "partNumber");
			if(StringUtils.isNotEmpty(partNumber)) {
				List<WTPart> parts = QueryUtil.getAllWTPartByName("", partNumber);
				List<String> views = new ArrayList<String>();
				if(null != parts && parts.size() > 0) {
					JSONArray array = new JSONArray();
					for(WTPart part : parts) {
						if(!views.contains(part.getViewName())) {
							views.add(part.getViewName());
						}
					}
					
					for(String view : views) {
						WTPart part = CoreUtil.getWTPartByNumberAndView(partNumber, view);
						getWTPartFile(part, array);
					}
					result = ChangeUtil.resSuccess(array, "", "暂无数据");
				} else {
					result = ChangeUtil.resFailed("料号" + partNumber + "不存在");
				}
			} else {
				result = ChangeUtil.resFailed("参数partNumber不能为空");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 获取指定类型的文档
	 * @param wtpart
	 * @return
	 * @throws JSONException
	 * @author xucr
	 * @CreateDate 2020年12月23日
	 */
	private void getWTPartFile(WTPart wtpart, JSONArray array) throws JSONException {
		if(null != wtpart) {
			try {
				//说明文档：图档类文档、工艺文件都放在物料说明文档中
				QueryResult qr= WTPartHelper.service.getDescribedByWTDocuments(wtpart);
				WTDocument doc = null;
				String typeName = "";
				JSONObject obj = null;
				while (qr.hasMoreElements()) {
					doc = (WTDocument) qr.nextElement();
					typeName = ChangeUtil.getSoftTypeName(doc, false);
					if((typeName.indexOf(ChangeConstants.DOCTYPES[0]) > -1 || typeName.indexOf(ChangeConstants.DOCTYPES[1]) > -1 )
							&& RELEASED.equals(doc.getState().getState().toString())) {
						obj = docInfo(doc, null);
						array.put(obj);
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 查询物料，用于进阶
	 * @param params
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月1日
	 */
	public JSONObject findFinishedWTPart(String data) {
		JSONObject result = null;
		try {
			JSONObject dataObj = new JSONObject(data);
			String partNumber = ChangeUtil.getJSONString(dataObj, "partNumber");
			String softType = ChangeUtil.getJSONString(dataObj, "softType");
			if(StringUtils.isEmpty(partNumber)) {
				result = ChangeUtil.resFailed("物料编码不能为空");
			} 
			String view = ChangeUtil.getJSONString(dataObj, "view");
			if(StringUtils.isEmpty(view)) {
				result = ChangeUtil.resFailed("视图不能为空");
			}
			if(StringUtils.isEmpty(softType)) {
				result = ChangeUtil.resFailed("物料类型不能为空");
			}
			
			if(null == result) {
				WTPart part = CoreUtil.getWTPartByNumberAndView(partNumber, view);
				//进阶的校验，参考进阶流程， VersionPromoteValidator.java
				if(null != part) {
					if( WorkInProgressHelper.isCheckedOut(part) ){
						result = ChangeUtil.resFailed("零部件被检出，不能进行版本进阶");
					} else {
						String _softType = ChangeUtil.getSoftTypeName(part, false); //拿到完整结构名称，如：wt.part.WTPart|cn.tianma.FinishedGood
						if(_softType.indexOf(softType) > -1) {
							//指定状态才能进阶：新品、量产发布、正在工作、批准
							String state = part.getLifeCycleState().toString();
							if(state.contains("NewProduct") || state.contains("MassProductionRelease")
									|| state.contains("APPROVAL") || state.contains("INWORK")) {
								JSONArray array = new JSONArray();
								JSONObject partObj = partInfo(part);
								array.put(partObj);
								result = ChangeUtil.resSuccess(array, array.length());
							} else {
								result = ChangeUtil.resFailed("仅【新品、量产发布、正在工作、批准】的状态下才能进阶");
							}
						} else {
							result = ChangeUtil.resFailed("当前料号【" + partNumber + "】与选择的物料类型不匹配");
						}
					}
				} else {
					result = ChangeUtil.resFailed("编码为【" + partNumber + "】的物料在PLM系统中不存在");
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (RemoteException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 获取对象所有属性，并包含变更前后数据
	 * @param partNumber
	 * @param view
	 * @param rdpId
	 * @param oid
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月1日
	 */
	public JSONObject getLayoutDefinition(String data) {
		JSONObject result = ChangeUtil.resSuccess0("暂无数据");
		try {
			JSONObject dataObj = new JSONObject(data);
			String partNumber = dataObj.getString("partNumber"); 
			String view = dataObj.getString("view");
			String rdpId = ChangeUtil.getJSONString(dataObj, "rdpId"); //非必传
			String oid = ChangeUtil.getJSONString(dataObj, "oid"); //非必传
			if(StringUtils.isNotEmpty(partNumber) && StringUtils.isNotEmpty(view)) {
				WTPart part = CoreUtil.getWTPartByNumberAndView(partNumber, view);
				if(null != part) {
					JSONArray array = new JSONArray();
					//当rdpID不为空时，传送给RDP的数据中，value值要为RDP操作的修改数据
					JSONObject changeObj = QueryUtil.getRDPChangeData(rdpId, oid, ChangeConstants.RDP_MODULE[0]);
					String tip = ChangeUtil.initPersistableAttr(part, array, changeObj);
					if(StringUtils.isNotEmpty(tip)) {
						result = ChangeUtil.resFailed(tip);
					} else {
						result = ChangeUtil.resSuccess(array, array.length());
					}
				} else {
					result = ChangeUtil.resFailed("视图为【"+view+"】的料号【"+partNumber+"】不存在");
				}
			} else {
				result = ChangeUtil.resFailed("物料编码和视图不能为空");
			}
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 根据物料查询视图
	 * @param number
	 * @param name
	 * @author xucr
	 * @CreateDate 2020年12月1日
	 */
	public JSONObject getViewByPart(String data) {
		JSONObject result = null;
		try {
			//编码或者名称，二者任意一个均可
			JSONObject dataObj = new JSONObject(data);
			String partNumber = ChangeUtil.getJSONString(dataObj, "partNumber");
			String partName = ChangeUtil.getJSONString(dataObj, "partName");
			if(StringUtils.isEmpty(partNumber) && StringUtils.isEmpty(partName)) {
				result = ChangeUtil.resFailed("编码和名称不能同时为空");
			} else {
				List<WTPart> parts = QueryUtil.getAllWTPartByName(partName, partNumber);
				JSONArray array = new JSONArray();
				List<String> views = new ArrayList<String>();
				if(parts.size() > 0) {
					for(WTPart part : parts) {
						if(!views.contains(part.getViewName())) {
							array.put(part.getViewName());
							views.add(part.getViewName());
						}
					}
					result = ChangeUtil.resSuccess(array, array.length());
				} else {
					result = ChangeUtil.resFailed("编码或名称对应的物料不存在");
				}
			}
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 查询物料列表
	 * @param partNumber
	 * @param partName
	 * @param view
	 * @param describe
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月1日
	 */
	public JSONObject findWTPart(String data) {
		JSONObject result = null;
		JSONObject params = new JSONObject();
		try {
			JSONObject dataObj = new JSONObject(data);
			String partNumber = ChangeUtil.getJSONString(dataObj, "partNumber");
			String partName = ChangeUtil.getJSONString(dataObj, "partName");
			String view = ChangeUtil.getJSONString(dataObj, "view");
			String partDescription = ChangeUtil.getJSONString(dataObj, "partDescription");
			if(StringUtils.isEmpty(partNumber) && StringUtils.isEmpty(partName)) {
				result = ChangeUtil.resFailed("编码和名称不能同时为空");
			} else {
				if(StringUtils.isEmpty(view)) {
					result = ChangeUtil.resFailed("视图不能为空");
				} else {
					params.put("number", TMUtil.conversion(partNumber));
					params.put("name", TMUtil.conversion(partName));
					params.put("view", TMUtil.conversion(view));
					params.put("partDescription", TMUtil.conversion(partDescription));
					
					QuerySpec qs = QueryUtil.queryWTPart(params);
					logger.info("findWTPart sql>>>" + qs.toString());
					QueryResult qr = PersistenceHelper.manager.find(qs);
					if(qr.size() > 0) {
						JSONArray array = new JSONArray();
						JSONObject obj = null;
						List<WTPart> parts = new ArrayList<WTPart>();
						while (qr.hasMoreElements()) {
							WTPart part = (WTPart) qr.nextElement();
							part = PartUtil.getLatestPartByPartMaster((WTPartMaster) part.getMasterReference().getObject());
							if(!parts.contains(part)) {
								parts.add(part);
								obj = partInfo(part);
								array.put(obj);
							}
						}
						result = ChangeUtil.resSuccess(array, array.length());
					} else {
						result = ChangeUtil.resSuccess0("暂无数据");
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (RemoteException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 根据rdpId查询变更信息
	 * @param rdpId
	 * @return
	 * @throws JSONException
	 * @author xucr
	 * @CreateDate 2020年12月9日
	 */
	public JSONObject findRDPChangeByRdpId(String rdpId, String module) throws JSONException {
		/**
		 * 根据rdpId，查询物料变更信息，返回到RDP
		 * 1、物料后 表格
		 * 		oid、名称、编码、视图、状态、版本号、物料描述、变更信息
		 * 		变更信息格式：属性A：由“历史值”变更为“新值”
		 * 
		 * 2、文档 表格
		 * */
		JSONObject resultObj = null;
		String result = "";
		JSONArray array = new JSONArray();
		try {
			JSONObject params = new JSONObject();
			params.put("module", module);
			if(StringUtils.isNotEmpty(rdpId)) {
				QuerySpec qs = QueryUtil.queryRDPChange(rdpId, params);
				logger.info("findRDPChangeByRdpId:qs >>> :" + qs.toString());
				QueryResult qr = PersistenceHelper.manager.find(qs);
				JSONObject obj = null;
				String changeData = "";
				JSONArray changeArray = null;
				WTUser user = null;
				WTUser creator = null;
				while(qr.hasMoreElements()) {
					RDPChange change = (RDPChange) qr.nextElement();
					if(null != change.getChangeInfo()) {
						changeData = ChangeUtil.blob2str(change.getChangeInfo());
					}
					String userName = change.getOwner();
					if(StringUtils.isNotEmpty(userName)) {
						user = UserUtil.getUserByName(userName);
					}
					
					String creatorName = change.getCreator();
					if(StringUtils.isNotEmpty(creatorName)) {
						creator = UserUtil.getUserByName(creatorName);
					}
					JSONObject creatorInfo = initRDPUser(creator);
					Persistable per = change.getPersistable().getObject();
					if(per instanceof WTPart) { //物料，则为物料变更
						WTPart part = (WTPart) per;
						obj = partInfo(part);
						changeArray = findChangeInfo(changeData, part);
						obj.put("changeInfo", changeArray);//变更信息
					} else if(per instanceof WTDocument) { //图档类文档
						WTDocument doc = (WTDocument) per;
						obj = docInfo(doc, user);
					}
					obj.put(ChangeConstants.CREATOR, creatorInfo);//创建者信息返回给RDP
					array.put(obj);
				}
			} else {
				result = "rdpId不能为空";
			}
			resultObj = ChangeUtil.resSuccess(array, result, "暂无数据");
		} catch (WTException e) {
			e.printStackTrace();
			resultObj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (RemoteException e) {
			e.printStackTrace();
			resultObj = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return resultObj;
	}
	
	/**
	 * 查询物料的变更信息
	 * @param changeData
	 * @param part
	 * @return
	 * @throws JSONException
	 * @throws RemoteException
	 * @throws WTException
	 * @author xucr
	 * @CreateDate 2020年12月16日
	 */
	public JSONArray findChangeInfo(String changeData, WTPart part) throws JSONException, RemoteException, WTException {
		List<String> ibaNames = Arrays.asList(ChangeConstants.IBANAMES); //这些iba属性key与全局属性名称不一致
		JSONArray changeArray = new JSONArray();
		if(StringUtils.isNotEmpty(changeData)) {
			JSONObject changeObject = new JSONObject(changeData);
			Iterator iter = changeObject.keys();
			JSONObject changeObj = null;
			
			TypeDefinitionReadView tv = TypeDefinitionServiceHelper.service.getTypeDefView(ChangeUtil.getSoftTypeName(part));
			String displayName = "";
			String history = "";
			while(iter.hasNext()) {
				changeObj = new JSONObject();
				String key = (String) iter.next();
				String changeValue = ChangeUtil.getJSONString(changeObject, key);
				AttributeDefDefaultView adv = IBAUtil.getAttributeDefinition(key); //得到IBA属性定义
				AttributeDefinitionReadView adr = tv.getAttributeByName(key);//对象属性
				if(ibaNames.contains(key)) {//特别处理，因为这些IBA属性与全局属性key不对应
					displayName = adv.getLocalizedDisplayString();
					history = (String) LWCUtil.getValue(part, key);
				} else if(null != adv) { //IBA属性
					displayName = adv.getLocalizedDisplayString();
					history = (String) IBAUtil.getIBAValue(part, key);
				} else if(null != adr) {//对象本身属性，如：名称、编号等，前提是能编辑的
					history = ChangeUtil.getValue(part, key);
					PropertyValueReadView propertyValueView = adr.getPropertyValueByName(ChangeUtil.PROP_DISPLAYNAME);
					if(null != propertyValueView) {
						displayName = String.valueOf(propertyValueView.getValue(Locale.getDefault(), false));
					}
				}
				changeObj.put("name", key);
				changeObj.put("displayName", displayName);
				changeObj.put("value", changeValue);
				changeObj.put("beforeValue", history);
				changeArray.put(changeObj);
			}
		}
		return changeArray;
	}
	
	/**
	 * 封装物料属性
	 * @param part
	 * @return
	 * @throws JSONException
	 * @throws RemoteException
	 * @throws WTException
	 * @author xucr
	 * @CreateDate 2020年12月9日
	 */
	private JSONObject partInfo(WTPart part) throws JSONException, RemoteException, WTException {
		JSONObject partObj = new JSONObject();
		partObj.put("partName", part.getName());
		partObj.put("partNumber", part.getNumber());
		partObj.put("oid", TMUtil.obj2Oid(part));
		partObj.put("view", part.getViewName());
		
		Hashtable table = IBAUtil.getIBAValues(part);
		partObj.put("partDescription", TMUtil.conversion((String) table.get("partDescription")));//物料描述
		partObj.put("materialDetails", TMUtil.conversion((String) table.get("materialDetails")));//物料详细说明
		partObj.put("finishedGoodDimension", TMUtil.conversion((String) table.get("finishedGoodDimension")));//产品维度 
		partObj.put("state", part.getState().getState().getDisplay(Locale.getDefault()));
		partObj.put("version", TMUtil.conversion((String)CommonPDMUtil.getVersion(part)));
		partObj.put("owner", part.getOwnership().getOwner().getName());
		String partNumberEffectiveSite = TMUtil.conversion((String) table.get("partNumberEffectiveSite"));
//		partObj.put("partNumberEffectiveSite", ChangeUtil.getEnumerValue(partNumberEffectiveSite, PARTNUMBEREFFECTIVESITE)); //料号生效产地
		partObj.put("partNumberEffectiveSite", partNumberEffectiveSite); //料号生效产地，前端要求返回key
		
		String time1 = TMUtil.timestampToString((Timestamp)table.get("MPTimeNew"), "yyyy-MM-dd");
		partObj.put("ModuleProductProperty", TMUtil.conversion((String) table.get("ModuleProductProperty")));//模组是否标准品
		partObj.put("panelPlaceOfOrigin", TMUtil.conversion((String) table.get("panelPlaceOfOrigin")));//panel产地
		partObj.put("ModuleOrigin", TMUtil.conversion((String) table.get("ModuleOrigin")));//模组组装产地,【注意，与属性  模组产地(moduleOrigin) 名称类似，注意首字母大写】
		partObj.put("ModuleLaminatingOrigin", TMUtil.conversion((String) table.get("ModuleLaminatingOrigin")));//模组贴合产地
		partObj.put("MPTimeNew", time1);//MP时间
		partObj.put("KickoffTimeNew", TMUtil.conversion((String) table.get("KickoffTimeNew")));//Kick-off时间，一个手动录入的字符串
		partObj.put("projectManager", TMUtil.conversion((String) table.get("cn.tianma.projectManager")));//项目经理
		
		return partObj;
	}
	
	/**
	 * 封装文档信息
	 * @param doc
	 * @return
	 * @throws JSONException
	 * @throws RemoteException
	 * @throws WTException
	 * @author xucr
	 * @CreateDate 2020年12月10日
	 */
	private JSONObject docInfo(WTDocument doc, WTUser user) throws JSONException, RemoteException, WTException {
		JSONObject docInfo = new JSONObject();
		docInfo.put("docName", doc.getName());
		docInfo.put("docNumber", doc.getNumber());
		docInfo.put("docType", ChangeUtil.getSoftTypeName(doc));
		docInfo.put("oid", TMUtil.obj2Oid(doc));
		docInfo.put("docFilePath", TMUtil.conversion((String) IBAUtil.getIBAValue(doc, "docFilePath")));//docFilePath
		docInfo.put("fileUrl", TMUtil.conversion((String) IBAUtil.getIBAValue(doc, "URL附件链接")));//URL附件链接
		docInfo.put("version", TMUtil.conversion((String)CommonPDMUtil.getVersion(doc)));//版本信息
		docInfo.put("productNumber", TMUtil.conversion((String)IBAUtil.getIBAValue(doc, "productNumber")));//成品编号
		docInfo.put("moduleOrigin", TMUtil.conversion((String)IBAUtil.getIBAValue(doc, "moduleOrigin")));//生产地
		docInfo.put("SmallDocType", TMUtil.conversion((String)IBAUtil.getIBAValue(doc, "SmallDocType")));//文档小类 
		docInfo.put("productType", TMUtil.conversion((String)IBAUtil.getIBAValue(doc, "productType")));//型号 
		docInfo.put("modulesDevelopedLand", TMUtil.conversion((String)IBAUtil.getIBAValue(doc, "modulesDevelopedLand")));//研发地 
		docInfo.put("Item", TMUtil.conversion((String)IBAUtil.getIBAValue(doc, "Item")));//物料编号 
		docInfo.put("owner", doc.getOwnership().getOwner().getName());
		
		JSONObject userInfo = initRDPUser(user);
		docInfo.put("personalInfo", userInfo);
		return docInfo;
	}
	
	/**
	 * 按照RDP的格式封装用户
	 * @param user
	 * @return
	 * @throws WTException
	 * @throws JSONException
	 * @author xucr
	 * @CreateDate 2021年2月1日
	 */
	public JSONObject initRDPUser(WTUser user) throws WTException, JSONException {
		JSONObject userInfo = new JSONObject();
		String name = "";
		String displayName = "";
		String email = "";
		if(null != user) {
			name = user.getName();
			displayName = user.getFullName();
			email = TMUtil.conversion(user.getEMail());
		}
		userInfo.put("displayName", displayName);
		userInfo.put("name", name);
		userInfo.put("email", email);
		return userInfo;
	}
	
	/**
	 * 物料反查
	 * @param number
	 * @param view
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月10日
	 */
	public JSONObject searhMaterialPart(String number,String view) {
		JSONObject obj = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			WTPart part1 = CoreUtil.getWTPartByNumberAndView(number, view); //当前原材料
			// 存放物料对应的父层 key - value集合 或半成品与成品对应的key - value集合 ,用于展示
			Map<WTPart, ArrayList<WTPart>> firstLevolmap = new HashMap<WTPart, ArrayList<WTPart>>();
			// 父层关系集合
			List<WTPart> parentlist = new ArrayList<WTPart>();
			List<WTPart> list = new ArrayList<WTPart>();
			list.add(part1);
			MaterialReverseService.getParentParts3(part1, parentlist, firstLevolmap,list);
			logger.info("firstLevolmap.size() :" + firstLevolmap.size());
				// 取物料的半成品或成品集合
			ArrayList<WTPart> parentslist = firstLevolmap.get(part1);
			JSONObject object = null;
			if (parentslist != null) {
				logger.info("parentslist.size() :" + parentslist.size());
				for (WTPart showpart : parentslist) {
					String softtype = GenericUtil.getTypeId(showpart);
					logger.info(showpart.getNumber() + "的类型为：" + softtype);
					if (OBSOLESCENCE.equals(showpart.getState().getState().toString())) { //废弃的不能使用
						continue;
					}
					// 如果是成品 直接写值
					if (softtype.indexOf(ChangeConstants.FINISHEDGOOD) != -1) {
						object = partInfo(showpart);
						object.put(RAWMATERIAL, number); //将查询条件原材料加入
						array.put(object);
						logger.info("第一次array：" + array.toString());
					} else {
						// 半成品 查找是否存在对应的成品
						ArrayList<WTPart> finishedlist = firstLevolmap.get(showpart);
						if (finishedlist != null && finishedlist.size() != 0) {
							logger.info("finishedlist.size() :" + finishedlist.size());
							for (WTPart halfpart : finishedlist) {
								String halfsofttype = GenericUtil.getTypeId(halfpart);
								if (OBSOLESCENCE.equals(halfpart.getState().getState().toString())) { //废弃的不能使用
									continue;
								}
								// 半成品存在相应的成品时
								if (halfsofttype.indexOf(ChangeConstants.FINISHEDGOOD) != -1) {
									if(!parentslist.contains(halfpart)){
										// 成品属性
										object = partInfo(halfpart);
										object.put(RAWMATERIAL, number); //将查询条件原材料加入
										array.put(object);
										logger.info("第二次array：" + array.toString());
									}
								} else {
									//继续往根节点取成品
									FinishedGoodArray(firstLevolmap,array,halfpart,part1);
									logger.info("第三次array：" + array.toString());
								}
							}
						}
					}
				}
			}
			obj = ChangeUtil.resSuccess(array, "", "暂无数据");
		} catch (WTException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (JSONException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (RemoteException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (Exception e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return obj;
	}
	
	/**
	 * 通过物料A查找非直接关系的成品D
	 * 如：A-->B-->C-->D
	 * 通过A找到半成品B 再间接找到成品D  B、D不存在直接关系
	 * @param firstLevolmap 
	 * @param array  显示的内容
	 * @param part   
	 * @param basicpart
	 * @return
	 * @throws Exception
	 */
	public void FinishedGoodArray(Map<WTPart, ArrayList<WTPart>> firstLevolmap,
			JSONArray array,WTPart part,WTPart basicpart) throws Exception{
		if(firstLevolmap.containsKey(part)){
			ArrayList<WTPart> finishedlist = firstLevolmap.get(part);
			JSONObject object = null;
			for (WTPart halfpart : finishedlist) {
				//类型
				String halfsofttype = GenericUtil.getTypeId(halfpart);
				if (OBSOLESCENCE.equals(halfpart.getState().getState().toString())) { //废弃的不能使用
					continue;
				}
				// 成品属性
				if (halfsofttype.indexOf(ChangeConstants.FINISHEDGOOD) != -1) {
					object = partInfo(halfpart);
					object.put(RAWMATERIAL, basicpart.getNumber()); //将查询条件原材料加入
					array.put(object);
				}else{
					FinishedGoodArray(firstLevolmap,array,halfpart,basicpart);
				} 
			}
		}
	}
	
	/**
	 * 保存RPD变更数据(单条保存)
	 * @param changeData
	 * @param rdpId
	 * @param oid
	 * @param module 模块名称
	 * @author xucr
	 * @CreateDate 2020年12月1日
	 */
	public JSONObject savePartToRDPChange(String data) {
		JSONObject result = new JSONObject();
		try {
			JSONObject dataObj = new JSONObject(data);
			String changeData = ChangeUtil.getJSONString(dataObj, "changeData");//不包含时，也返回空值
			String rdpId = dataObj.getString("rdpId");//不包含时，catch，返回前端，必须
			String oid = dataObj.getString("oid");
			String owner = ChangeUtil.getJSONString(dataObj, "owner");
			String creator =  ChangeUtil.getJSONString(dataObj, ChangeConstants.CREATOR);
			if(StringUtils.isNotEmpty(rdpId) && StringUtils.isNotEmpty(oid) && StringUtils.isNotEmpty(owner)
					&& StringUtils.isNotEmpty(changeData) && StringUtils.isNotEmpty(creator)) {
				WTPart p = (WTPart) TMUtil.oid2Obj(oid);
				boolean ischeck = WorkflowUtil.isObjectCheckedOut(p);
				if(ischeck) {
					result = ChangeUtil.resFailed("物料" + p.getNumber() + "已被检出，不能添加");
				} else {
					String module = ChangeConstants.RDP_MODULE[0]; //物料变更
					RDPChange change = QueryUtil.findRDPChange(p, rdpId, module);
					if(null != change) { //
						PersistenceHelper.manager.delete(change);
					}
					//有可能是RDP变更数据后，再次变更，windchill需要存储最终数据，因此需要将过程数据删除依次
					JSONObject params = new JSONObject();
					params.put(ChangeConstants.MODULE, module);
					params.put("persistableId", p.getPersistInfo().getObjectIdentifier().getId());
					params.put(ChangeConstants.CREATOR, creator);
					WTHashSet delSet = QueryUtil.findRDPChange(rdpId, params);
					if(delSet.size() > 0) {
						PersistenceHelper.manager.delete(delSet);
					}
					
					ChangeVO vo = new ChangeVO(rdpId, changeData, module, owner, creator, p);
					RDPChange addChange = QueryUtil.saveRDPChange(vo);
					if(null != addChange) {
						result = ChangeUtil.resSuccess("变更数据存储成功");
					} else {
						result = ChangeUtil.resFailed("接口异常，请联系系统管理员");
					}
				}
			} else {
				result = ChangeUtil.resFailed("rdpId、oid、changeData、creator、owner不能为空");
			}
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 保存RPD文档数据
	 * @param changeData
	 * @param rdpId
	 * @param oid
	 * @param module 模块名称
	 * @author xucr
	 * @CreateDate 2020年12月1日
	 */
	public JSONObject saveDocToRDPChange(String data) {
		JSONObject result = new JSONObject();
		try {
			JSONObject dataObj = new JSONObject(data);
			String rdpId = ChangeUtil.getJSONString(dataObj, ChangeConstants.RDPID);//不包含时，catch，返回前端，必须
			String oid = ChangeUtil.getJSONString(dataObj, "oid");
			String owner = ChangeUtil.getJSONString(dataObj, ChangeConstants.OWNER);//责任人
			String creator = ChangeUtil.getJSONString(dataObj, ChangeConstants.CREATOR);//数据创建者
			if(StringUtils.isNotEmpty(rdpId) && StringUtils.isNotEmpty(oid) 
					&& StringUtils.isNotEmpty(owner) && StringUtils.isNotEmpty(creator)) {
				String module = ChangeConstants.RDP_MODULE[1]; //文档内容
				Persistable per = TMUtil.oid2Obj(oid);
				//先删除
				JSONObject params = new JSONObject();
				params.put(ChangeConstants.MODULE, module);
				params.put("persistableId", per.getPersistInfo().getObjectIdentifier().getId());
				params.put(ChangeConstants.CREATOR, creator);
				WTHashSet delSet = QueryUtil.findRDPChange(rdpId, params);
				if(delSet.size() > 0) {
					PersistenceHelper.manager.delete(delSet);
				}
				List<WTUser> users = ProjectUtil.getUserList(owner);
				if(users.size() > 0) {
					Persistable p = TMUtil.oid2Obj(oid);
					ChangeVO vo = new ChangeVO(rdpId, "", module, owner, creator, p);
					RDPChange change = QueryUtil.saveRDPChange(vo);
					if(null != change) {
						result = ChangeUtil.resSuccess("文档数据存储成功");
					} else {
						result = ChangeUtil.resFailed("接口异常，请联系系统管理员");
					}
				} else {
					result = ChangeUtil.resFailed("用户" + owner + "不存在或已离职");
				}
			} else {
				result = ChangeUtil.resFailed("参数rdpId、oid、owner、creator不能为空");
			}
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return result;
	}
	
	/**
	 * 删除rdpchange
	 * @param data
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月14日
	 */
	public JSONObject delRDPChange(String data) {
		JSONObject result = null;
		try {
			if(StringUtils.isNotEmpty(data)){
				JSONObject dataObj = new JSONObject(data);
				String oid = ChangeUtil.getJSONString(dataObj, "oid");//RDPChange关联的对象
				String rdpId = ChangeUtil.getJSONString(dataObj, "rdpId");
				
				if(StringUtils.isNotEmpty(oid) && StringUtils.isNotEmpty(rdpId)) {
					Persistable per = TMUtil.oid2Obj(oid);
					
					Long id = per.getPersistInfo().getObjectIdentifier().getId();
					JSONObject params = new JSONObject();
					params.put("persistableId", id);
					QuerySpec qs = QueryUtil.queryRDPChange(rdpId, params);
					logger.info("delRDPChange qs:" + qs.toString());
					QueryResult qr = PersistenceHelper.manager.find(qs);
					WTHashSet set = new WTHashSet();
					while(qr.hasMoreElements()) {
						RDPChange change = (RDPChange) qr.nextElement();
						set.add(change);
					}
					if(set.size() > 0) {
						PersistenceHelper.manager.delete(set);
						result = ChangeUtil.resSuccess("操作成功");
					} else {
						result = ChangeUtil.resSuccess("操作成功，但无数据删除");
					}
				} else {
					result = ChangeUtil.resFailed("参数oid、rdpId不能为空");
				}
			} else {
				result = ChangeUtil.resFailed("数据传输不能为空");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**----------------------------------------------------------------BOM操作---------------------------------------------------------------*/
	/**
	 * 通用属性
	 * @param vo
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月3日
	 */
	public RDPBOMModel newRDPBOMModel(String rdpId, String effectNumber, String view, String creator) {
		RDPBOMModel model = null;
		try {
			WTPart part = CoreUtil.getWTPartByNumberAndView(effectNumber, view);
			if(null != part) {
				model = RDPBOMModel.newRDPBOMModel();
				model.setRdpId(rdpId);
				model.setECOAffectedRef(ObjectReference.newObjectReference(part));
				model.setECOView(view);
				model.setStatus(OPEN);
				model.setCreator(creator);
			}
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		return model;
	}
	
//	/**
//	 * BOM操作入口（九大类型批量）
//	 * @param data
//	 * @param bomOption
//	 * @return
//	 * @author xucr
//	 * @CreateDate 2020年12月5日
//	 */
//	public JSONObject saveEntrance(String data) {
//		JSONObject result = null;
//		try {
//			if(StringUtils.isNotEmpty(data)) {
//				JSONObject obj = new JSONObject(data);
//				String rdpId = ChangeUtil.getJSONString(obj, "rdpId");
//				String dataInfo = ChangeUtil.getJSONString(obj, "data");
//				if(StringUtils.isNotEmpty(rdpId) && StringUtils.isNotEmpty(dataInfo)) {
//					//先删除
//					JSONObject params = new JSONObject();
////					params.put("ecooperatingtype", bomOption);
//					params.put("rdpId", rdpId);
//					WTHashSet models = QueryUtil.findRDPBOMModel(rdpId, params);
//					if(models.size() > 0) {
//						PersistenceHelper.manager.delete(models);
//					}
//					//再创建
//					JSONArray array = new JSONArray(dataInfo);
//					WTHashSet set = new WTHashSet();
//					for(int i = 0 ; i < array.length(); i++) {
//						JSONObject childObj = array.getJSONObject(i);
//						String bomOption = ChangeUtil.getJSONString(childObj, "bomOption");
//						if(childObj.has("data")) {
//							JSONArray childArray = childObj.getJSONArray("data");
//							if(StringUtils.isNotEmpty(bomOption)) {
//								if(null != childArray) {
//									if(ChangeConstants.ADDPart.equals(bomOption))
//										result = addPart(rdpId, childArray, set);
//									else if(ChangeConstants.DelPart.equals(bomOption))
//										result = delPart(rdpId, childArray, set);
//									else if(ChangeConstants.ReplacePart.equals(bomOption))
//										result = replacePart(rdpId, childArray, set);
//									else if(ChangeConstants.UpdatePart.equals(bomOption))
//										result = modifyAmount(rdpId, childArray, set);
//									else if(ChangeConstants.ADDReplcePart.equals(bomOption))
//										result = addReplacePart(rdpId, childArray, set);
//									else if(ChangeConstants.DelReplcePart.equals(bomOption))
//										result = delReplacePart(rdpId, childArray, set);
//									else if(ChangeConstants.UpdateReplcePart.equals(bomOption))
//										result = replaceOldPart(rdpId, childArray, set);
//									else if(ChangeConstants.UpdateAPart.equals(bomOption))
//										result = repalcePartMain(rdpId, childArray, set);
//									else if(ChangeConstants.ReplaceABPart.equals(bomOption))
//										result = convertPartMain(rdpId, childArray, set);
//								}
//							} else {
//								result = ChangeUtil.resFailed("请检查，存在参数bomOption为空的条目");
//								break;
//							}
//						}
//					}
//					
//					if(set.size() > 0 && null == result) {
//						PersistenceHelper.manager.save(set);
//						result = ChangeUtil.resSuccess("操作成功");
//					}
//				} else {
//					result = ChangeUtil.resFailed("参数rdpId、data不能为空");
//				}
//			} else {
//				result = ChangeUtil.resFailed("数据传输不能为空");
//			}
//		} catch (JSONException e) {
//			e.printStackTrace();
//			result = ChangeUtil.resFailed(e.getLocalizedMessage());
//		} catch (WTException e) {
//			e.printStackTrace();
//			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
//		}
//		return result;
//	}
	
	/**
	 * BOM操作入口（单个类型批量）
	 * @param data
	 * @param bomOption
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject saveEntrance(String data) {
		JSONObject result = null;
		try {
			if(StringUtils.isNotEmpty(data)) {
				JSONObject obj = new JSONObject(data);
				String rdpId = ChangeUtil.getJSONString(obj, "rdpId");
				String bomOption = ChangeUtil.getJSONString(obj, "bomOption");
				String dataInfo = ChangeUtil.getJSONString(obj, "data");
				
				if(StringUtils.isNotEmpty(rdpId) && StringUtils.isNotEmpty(dataInfo) && StringUtils.isNotEmpty(bomOption)) {
					WTHashSet set = new WTHashSet();
					JSONArray array = new JSONArray(dataInfo);
					if(ChangeConstants.ADDPart.equals(bomOption))
						result = addPart(rdpId, array, set);
					else if(ChangeConstants.DelPart.equals(bomOption))
						result = delPart(rdpId, array, set);
					else if(ChangeConstants.ReplacePart.equals(bomOption))
						result = replacePart(rdpId, array, set);
					else if(ChangeConstants.UpdatePart.equals(bomOption))
						result = modifyAmount(rdpId, array, set);
					else if(ChangeConstants.ADDReplcePart.equals(bomOption))
						result = addReplacePart(rdpId, array, set);
					else if(ChangeConstants.DelReplcePart.equals(bomOption))
						result = delReplacePart(rdpId, array, set);
					else if(ChangeConstants.UpdateReplcePart.equals(bomOption))
						result = replaceOldPart(rdpId, array, set);
					else if(ChangeConstants.UpdateAPart.equals(bomOption))
						result = repalcePartMain(rdpId, array, set);
					else if(ChangeConstants.ReplaceABPart.equals(bomOption))
						result = convertPartMain(rdpId, array, set);
					else if(ChangeConstants.UpdateSubstitutesQuantity.equals(bomOption))
						result = modifySubstitutesQuantity(rdpId, array, set);
					
					if(set.size() > 0 && null == result) {
						PersistenceHelper.manager.save(set);
						result = ChangeUtil.resSuccess("操作成功");
					}
				} else {
					result = ChangeUtil.resFailed("参数rdpId、data、bomOption不能为空");
				}
			} else {
				result = ChangeUtil.resFailed("数据传输不能为空");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * BOM变更：新增物料
	 * @param vo
	 * @author xucr
	 * @CreateDate 2020年12月3日
	 */
	public JSONObject addPart(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String addNum = obj.getString("addNum");
					String amount = obj.getString("addAmount");
					
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(addNum);
						model.setECODosage(amount);
						model.setECOOperatingType(ChangeConstants.ADDPart);
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} 
		return result;
	}
	
	/**
	 * 删除料号
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月3日
	 */
	public JSONObject delPart(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String delNum = obj.getString("delNum");
					String delLineNumber = obj.getString("delLineNumber");//行号
					String delAmount = obj.getString("delAmount");//用量
					//如果存在oid，则编辑，否则创建
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(delNum);
						model.setECOOperatingType(ChangeConstants.DelPart);
						model.setECOLineNumber(delLineNumber);
						model.setECODosage(delAmount);
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 替换物料
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月4日
	 */
	public JSONObject replacePart(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String oldNum = obj.getString("oldNum");
					String oldAmount = obj.getString("oldAmount");
					String oldLineNumber = obj.getString("oldLineNumber");
					String newNum = obj.getString("newNum");
					String newAmount = obj.getString("newAmount");
					String newLineNumber = obj.getString("newLineNumber");
					
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(oldNum);
						model.setECODosage(oldAmount);
						model.setECOOperatingType(ChangeConstants.ReplacePart);
						model.setECOLineNumber(oldLineNumber);
						model.setECONewDosage(newAmount);
						model.setECONewLineNumber(newLineNumber);
						model.setECONewPartNumber(newNum);
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 修改用量
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject modifyAmount(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String lineNumber = obj.getString("oldLineNumber");
					String oldNum = obj.getString("oldNum");
					String oldAmount = obj.getString("oldAmount");
					String newAmount = obj.getString("newAmount");

					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(oldNum);
						model.setECOOperatingType(ChangeConstants.UpdatePart);
						model.setECODosage(oldAmount);
						model.setECONewDosage(newAmount);
						model.setECOLineNumber(lineNumber);
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * BOM：新增替代料
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject addReplacePart(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String mainNum = obj.getString("mainNum");
					String mainLineNumber = obj.getString("mainLineNumber");
					String newNum = obj.getString("newNum");
					String newAmount = obj.getString("newAmount");
					
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(mainNum);
						model.setECOOperatingType(ChangeConstants.ADDReplcePart);
						model.setECOLineNumber(mainLineNumber);
						model.setECOSubstituteNumber(newNum);
						model.setECOSubstituteDosage(newAmount);
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 删除替代料
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject delReplacePart(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String mainNum = obj.getString("mainNum");
					String mainLineNumber = obj.getString("mainLineNumber");
					
					String delNum = obj.getString("delNum");
					
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(mainNum);
						model.setECOOperatingType(ChangeConstants.DelReplcePart);
						model.setECOLineNumber(mainLineNumber);
						model.setECOSubstituteNumber(delNum);
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 更换替代料功能
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject replaceOldPart(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String mainNum = obj.getString("mainNum");
					String mainLineNumber = obj.getString("mainLineNumber");
					
					String oldNum = obj.getString("oldNum");
					String oldAmount = obj.getString("oldAmount");
					
					String newNum = obj.getString("newNum");
					String newAmount = obj.getString("newAmount");
					
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(mainNum);
						model.setECOOperatingType(ChangeConstants.UpdateReplcePart);
						model.setECOLineNumber(mainLineNumber);
						model.setECOSubstituteDosage(oldAmount); //变更前用量
						model.setECOSubstituteNumber(oldNum);//原料
						model.setECONewSubstituteNumber(newNum);//变更后料号
						model.setECONewSubstituteDosage(newAmount);//新用量
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 更换主料功能
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject repalcePartMain(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String oldNum = obj.getString("oldNum");
					String oldAmount = obj.getString("oldAmount");
					String oldLineNumber = obj.getString("oldLineNumber");
					String newNum = obj.getString("newNum");
					String newAmount = obj.getString("newAmount");
					
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(oldNum);
						model.setECOOperatingType(ChangeConstants.UpdateAPart);
						model.setECODosage(oldAmount); //变更前用量
						model.setECOLineNumber(oldLineNumber); //行号
						model.setECONewPartNumber(newNum); //新料号
						model.setECONewDosage(newAmount);//新用量
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 主次替换功能
	 * @param rdpId
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月5日
	 */
	public JSONObject convertPartMain(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String mainNum = obj.getString("mainNum");
					String mainLineNumber = obj.getString("mainLineNumber");
					String oldNum = obj.getString("oldNum");
					
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(mainNum);
						model.setECOOperatingType(ChangeConstants.ReplaceABPart);
						model.setECOLineNumber(mainLineNumber);
						model.setECONewPartNumber(oldNum);
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 替代料用量修改
	 * @param rdpId
	 * @param array
	 * @param set
	 * @return
	 * @author xucr
	 * @CreateDate 2021年1月20日
	 */
	public JSONObject modifySubstitutesQuantity(String rdpId, JSONArray array, WTHashSet set) {
		JSONObject result = null;
		try {
			JSONObject obj = null;
			if(null != array && StringUtils.isNotEmpty(rdpId)) {
				for(int i = 0 ; i < array.length(); i++) {
					obj = array.getJSONObject(i);
					String mainNum = obj.getString("mainNum");
					String mainLineNumber = obj.getString("mainLineNumber");
					
					String oldNum = obj.getString("oldNum");
					String oldAmount = obj.getString("oldAmount");
					
					String newAmount = obj.getString("newAmount");
					RDPBOMModel model = initRDPBOMMolde(obj, rdpId);
					if(null != model) {
						model.setECOPartNumber(mainNum);
						model.setECOOperatingType(ChangeConstants.UpdateSubstitutesQuantity);
						model.setECOLineNumber(mainLineNumber);
						model.setECOSubstituteDosage(oldAmount); //旧用量
						model.setECOSubstituteNumber(oldNum);//替代料
						model.setECONewSubstituteDosage(newAmount);//新用量
						
						set.add(model);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 初始化对象
	 * @param obj
	 * @param rdpId
	 * @return
	 * @throws JSONException
	 * @author xucr
	 * @CreateDate 2020年12月24日
	 */
	private RDPBOMModel initRDPBOMMolde(JSONObject obj, String rdpId) throws JSONException {
		//如果存在oid，则返回该对象，否则新建
		String oid = ChangeUtil.getJSONString(obj, "oid");
		RDPBOMModel model = null;
		if(StringUtils.isNotEmpty(oid)) {
			model = (RDPBOMModel) TMUtil.oid2Obj(oid);
		} else {
			String effectNumber = ChangeUtil.getJSONString(obj, "effectNumber");
			String view = ChangeUtil.getJSONString(obj, "view");
			String creator = ChangeUtil.getJSONString(obj, ChangeConstants.CREATOR);
			model = newRDPBOMModel(rdpId, effectNumber, view, creator);
		}
		return model;
	}
	
	/**
	 * 查询BOM变更模块数据
	 * @param rdpId
	 * @param bomOption
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月8日
	 */
	public JSONObject queryRDPBOMDataDetail(String rdpId, String bomOption) {
		JSONObject obj = null;
		JSONArray array = new JSONArray(); //数据
		try {
			JSONObject params = new JSONObject();
			params.put("ecooperatingtype", bomOption);
			QuerySpec qs = QueryUtil.queryRDPBOMModel(rdpId, params);
			logger.info("queryRDPBOMDataDetail qs:" + qs.toString());
			QueryResult qr = PersistenceHelper.manager.find(qs);
			
			JSONObject bomObj = null;
			WTUser creator = null;
			while(qr.hasMoreElements()) {
				bomObj = new JSONObject();
				RDPBOMModel model = (RDPBOMModel) qr.nextElement();
				WTPart effectPart = (WTPart) model.getECOAffectedRef().getObject();
				
				if(ChangeConstants.ADDPart.equals(bomOption)) {
					WTPart childPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("effectState", effectPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("effectUnit", effectPart.getDefaultUnit());
					bomObj.put("view", model.getECOView());
					bomObj.put("unit", childPart.getDefaultUnit());
					bomObj.put("state", childPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("addNum", childPart.getNumber());
					bomObj.put("addAmount", model.getECODosage());
				}
				else if(ChangeConstants.DelPart.equals(bomOption)) {
					WTPart childPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("effectState", effectPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("view", model.getECOView());
					bomObj.put("delNum", model.getECOPartNumber());//删除料料号
					bomObj.put("delLineNumber", model.getECOLineNumber());//子料行号
					bomObj.put("delAmount", model.getECODosage());//子料数量
					bomObj.put("delUnit", childPart.getDefaultUnit());//子料单位
					bomObj.put("delState", childPart.getState().getState().getDisplay(Locale.getDefault()));//子料状态
				}
				else if(ChangeConstants.ReplacePart.equals(bomOption)) {
					WTPart oldPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					WTPart newPart = CoreUtil.getWTPartByNumberAndView(model.getECONewPartNumber(), model.getECOView());
					
					bomObj = new JSONObject();
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("view", model.getECOView());
					bomObj.put("oldNum", oldPart.getNumber());
					bomObj.put("oldAmount", model.getECODosage());
					bomObj.put("oldLineNumber", model.getECOLineNumber());
					bomObj.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("oldUnit", oldPart.getDefaultUnit());
					bomObj.put("newAmount", model.getECONewDosage()); // 新用量
					bomObj.put("newLineNumber", model.getECONewLineNumber()); //新行号
					bomObj.put("newNum", newPart.getNumber());
					bomObj.put("newState", newPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("newUnit", newPart.getDefaultUnit());
				}
				else if(ChangeConstants.UpdatePart.equals(bomOption)) {
					WTPart oldPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					
					bomObj.put("oldNum", oldPart.getNumber());
					bomObj.put("oldAmount", model.getECODosage());
					bomObj.put("oldLineNumber", model.getECOLineNumber());
					bomObj.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("oldUnit", oldPart.getDefaultUnit());
					bomObj.put("newAmount", model.getECONewDosage());
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("view", model.getECOView());
				}
				else if(ChangeConstants.ADDReplcePart.equals(bomOption)) {
					WTPart mainPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					WTPart partB = CoreUtil.getWTPartByNumberAndView(model.getECOSubstituteNumber(), model.getECOView());
					
					bomObj.put("mainNum", model.getECOPartNumber());
					bomObj.put("mainLineNumber", model.getECOLineNumber());
					bomObj.put("mainUnit", mainPart.getDefaultUnit());
					bomObj.put("mainState", mainPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("newNum", partB.getNumber());
					bomObj.put("newAmount", model.getECOSubstituteDosage());
					bomObj.put("newUnit", partB.getDefaultUnit());
					bomObj.put("newState", partB.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("view", model.getECOView());
				}
				else if(ChangeConstants.DelReplcePart.equals(bomOption)) {
					WTPart delPart = CoreUtil.getWTPartByNumberAndView(model.getECOSubstituteNumber(), model.getECOView());
					WTPart mainPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("effectState", effectPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("delNum", model.getECOSubstituteNumber());
					bomObj.put("delUnit", delPart.getDefaultUnit());
					bomObj.put("delState", delPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("mainUnit", mainPart.getDefaultUnit());
					bomObj.put("mainState", mainPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("mainNum", model.getECOPartNumber());
					bomObj.put("mainLineNumber", model.getECOLineNumber());
					bomObj.put("view", model.getECOView());
				}
				else if(ChangeConstants.UpdateReplcePart.equals(bomOption)) {
					WTPart oldPart = CoreUtil.getWTPartByNumberAndView(model.getECOSubstituteNumber(), model.getECOView());
					WTPart newPart = CoreUtil.getWTPartByNumberAndView(model.getECONewSubstituteNumber(), model.getECOView());
					WTPart mainPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("effectState", effectPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("effectUnit", effectPart.getDefaultUnit());
					bomObj.put("view", model.getECOView());
					bomObj.put("mainState", mainPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("mainNum", model.getECOPartNumber());
					bomObj.put("mainLineNumber", model.getECOLineNumber());
					bomObj.put("oldNum", model.getECOSubstituteNumber());
					bomObj.put("oldAmount", model.getECOSubstituteDosage());
					bomObj.put("oldUnit", oldPart.getDefaultUnit());
					bomObj.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("newNum", model.getECONewSubstituteNumber());
					bomObj.put("newAmount", model.getECONewSubstituteDosage());
					bomObj.put("newState", newPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("newUnit", newPart.getDefaultUnit());
				}
				else if(ChangeConstants.UpdateAPart.equals(bomOption)) {
					WTPart oldPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					WTPart newPart = CoreUtil.getWTPartByNumberAndView(model.getECONewPartNumber(), model.getECOView());
					
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("effectState", effectPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("view", model.getECOView());
					bomObj.put("oldNum", model.getECOPartNumber());
					bomObj.put("oldAmount", model.getECODosage());
					bomObj.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("oldUnit", oldPart.getDefaultUnit());
					bomObj.put("oldLineNumber", model.getECOLineNumber());
					bomObj.put("newNum", model.getECONewPartNumber());
					bomObj.put("newAmount", model.getECONewDosage());
					bomObj.put("newState", newPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("newUnit", newPart.getDefaultUnit());
				}
				else if(ChangeConstants.ReplaceABPart.equals(bomOption)) {
					WTPart mainPart = CoreUtil.getWTPartByNumberAndView(model.getECOPartNumber(), model.getECOView());
					WTPart newPart = CoreUtil.getWTPartByNumberAndView(model.getECONewPartNumber(), model.getECOView());
					
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("effectState", effectPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("view", model.getECOView());
					
					bomObj.put("mainNum", model.getECOPartNumber());
					bomObj.put("mainUnit", mainPart.getDefaultUnit());
					bomObj.put("mainState", mainPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("mainLineNumber", model.getECOLineNumber());
					
					bomObj.put("oldNum", model.getECONewPartNumber());
					bomObj.put("oldUnit", newPart.getDefaultUnit());
					bomObj.put("oldState", newPart.getState().getState().getDisplay(Locale.getDefault()));
				} else if(ChangeConstants.UpdateSubstitutesQuantity.equals(bomOption)) {
					WTPart oldPart = CoreUtil.getWTPartByNumberAndView(model.getECOSubstituteNumber(), model.getECOView());
					
					bomObj.put("effectNumber", effectPart.getNumber());
					bomObj.put("effectState", effectPart.getState().getState().getDisplay(Locale.getDefault()));
					bomObj.put("view", model.getECOView());
					
					bomObj.put("mainNum", model.getECOPartNumber());
					
					bomObj.put("oldNum", model.getECOSubstituteNumber());
					bomObj.put("unit", oldPart.getDefaultUnit());
					bomObj.put("oldAmount", model.getECOSubstituteDosage());
					bomObj.put("oldState", oldPart.getState().getState().getDisplay(Locale.getDefault()));
					
					bomObj.put("newAmount", model.getECONewSubstituteDosage());
				}
				bomObj.put("oid", TMUtil.obj2Oid(model));
				bomObj.put(OBJOID, TMUtil.obj2Oid(effectPart));//受影响对象oid
				String partNumberEffectiveSite = TMUtil.conversion((String) IBAUtil.getIBAValue(effectPart, "partNumberEffectiveSite"));
//				bomObj.put("partNumberEffectiveSite", ChangeUtil.getEnumerValue(partNumberEffectiveSite, PARTNUMBEREFFECTIVESITE)); //料号生效产地
				bomObj.put("partNumberEffectiveSite", partNumberEffectiveSite); //料号生效产地，前端要求返回key
				
				if(StringUtils.isNotEmpty(model.getCreator())) {
					creator = UserUtil.getUserByName(model.getCreator());
				}
				JSONObject creatorInfo = initRDPUser(creator);
				bomObj.put(ChangeConstants.CREATOR, creatorInfo);
				array.put(bomObj);
			}
			obj = ChangeUtil.resSuccess(array, "", "暂无数据");
		} catch (JSONException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		} catch (RemoteException e) {
			e.printStackTrace();
			obj = ChangeUtil.resFailed(e.getLocalizedMessage());
		}
		return obj;
	}
	
	/**
	 * 删除BOM
	 * @param data
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月14日
	 */
	public JSONObject delBOMData(String data) {
		JSONObject result = null;
		try {
			if(StringUtils.isNotEmpty(data)){
				JSONObject dataObj = new JSONObject(data);
				String oid = ChangeUtil.getJSONString(dataObj, "oid");//RDPBOMModel对象
				
				//针对RDPBOMModel的oid删除对象，只会有一条
				RDPBOMModel model = (RDPBOMModel) TMUtil.oid2Obj(oid);
				PersistenceHelper.manager.delete(model);
				result = ChangeUtil.resSuccess("操作成功");
				
//				//针对RDPBOMModel的受影响对象删除，可能有多条
//				String rdpId = ChangeUtil.getJSONString(dataObj, "rdpId");
//				String objOid = ChangeUtil.getJSONString(dataObj, OBJOID);//受影响对象oid
//				Persistable per = TMUtil.oid2Obj(objOid);
//				if(null != per) {
//					Long id = per.getPersistInfo().getObjectIdentifier().getId();
//					JSONObject params = new JSONObject();
//					params.put("persistableId", id);
//					params.put("rdpId", rdpId);
//					
//					QuerySpec qs = QueryUtil.queryRDPBOMModel(rdpId, params);
//					logger.info("delBOMData qs:" + qs.toString());
//					QueryResult qr = PersistenceHelper.manager.find(qs);
//					WTHashSet set = new WTHashSet();
//					while(qr.hasMoreElements()) {
//						RDPBOMModel _model = (RDPBOMModel) qr.nextElement();
//						set.add(_model);
//					}
//					if(set.size() > 0) {
//						PersistenceHelper.manager.delete(set);
//						result = ChangeUtil.resSuccess("操作成功");
//					} else {
//						result = ChangeUtil.resSuccess("操作成功，但无数据删除");
//					}
//				}
			} else {
				result = ChangeUtil.resFailed("数据传输不能为空");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
		}
		return result;
	}
	
	/**
	 * 设置值
	 * @param rdpmodel
	 * @param model
	 * @author xucr
	 * @CreateDate 2020年12月12日
	 */
	public void seteco(RDPBOMModel rdpmodel, TMECOITEMModel model, WTChangeOrder2 eco) {
		try {
			ObjectReference partRef = rdpmodel.getECOAffectedRef();
			if(null != rdpmodel.getECOOperatingType()) {
				model.setECOOperatingType(rdpmodel.getECOOperatingType());
			}
			if(null != partRef) {
				model.setECOAffectedRef(partRef);
			}
			if(null != rdpmodel.getECOView()) {
				model.setECOView(rdpmodel.getECOView());
			}
			if(null != rdpmodel.getECOPartNumber()) {
				model.setECOPartNumber(rdpmodel.getECOPartNumber());
			}
			if(null != rdpmodel.getECODosage()) {
				model.setECODosage(rdpmodel.getECODosage());
			}
			if(null != rdpmodel.getECOLineNumber()) {
				model.setECOLineNumber(rdpmodel.getECOLineNumber());
			}
			if(null != rdpmodel.getECONewPartNumber()) {
				model.setECONewPartNumber(rdpmodel.getECONewPartNumber());
			}
			if(null != rdpmodel.getECONewDosage()) {
				model.setECONewDosage(rdpmodel.getECONewDosage());
			}
			if(null != rdpmodel.getECONewLineNumber()) {
				model.setECONewLineNumber(rdpmodel.getECONewLineNumber());
			}
			if(null != rdpmodel.getECOSubstituteNumber()) {
				model.setECOSubstituteNumber(rdpmodel.getECOSubstituteNumber());
			}
			if(null != rdpmodel.getECOSubstituteLineNmuber()) {
				model.setECOSubstituteLineNmuber(rdpmodel.getECOSubstituteLineNmuber());
			}
			if(null != rdpmodel.getECOSubstituteDosage()) {
				model.setECOSubstituteDosage(rdpmodel.getECOSubstituteDosage());
			}
			if(null != rdpmodel.getECONewSubstituteNumber()) {
				model.setECONewSubstituteNumber(rdpmodel.getECONewSubstituteNumber());
			}
			if(null != rdpmodel.getECONewSubstituteLineNmuber()) {
				model.setECONewSubstituteLineNmuber(rdpmodel.getECONewSubstituteLineNmuber());
			}
			if(null != rdpmodel.getECONewSubstituteDosage()) {
				model.setECONewSubstituteDosage(rdpmodel.getECONewSubstituteDosage());
			}
			
			/**
			 * 设置ECO对象属性
			 * ECORef
			 * ECONumber
			 * ECOStatus
			 * str1 -- str2 ： 
			 * */
			model.setNumber(TMObjectUtilHelper.getTMECOITEMAutoNumber());
			model.setName(eco.getNumber());
			// ECO
			model.setECORef(ObjectReference.newObjectReference(eco));
			// eco编号
			model.setECONumber(eco.getNumber());
			model.setStr1(((WTPart)partRef.getObject()).getNumber()); //受影响对象编号
			model.setECOStatus(OPEN);
			model.setStr2("1");// 批量变更
			model.setContainerReference(eco.getContainerReference());
			
			//按照原逻辑，放入文件夹
			Folder folder = FolderUtil.getFolder("/Default/RDP", eco.getContainerReference());
			if (folder != null) {
				WTList documentsList = new WTArrayList();
				documentsList.add(model);
				FolderHelper.assignLocation(documentsList, folder);
			}
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
	}
	
	/**-------------------------------------------------------------接收RDP表单数据，发起流程----------------------------------------------------------*/
	
	/**
	 * 接收RDP信息，发起各种流程
	 * @param data
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月11日
	 */
	public JSONObject submitRDPForm(String data) {
		JSONObject result = null;
		try {
			if(StringUtils.isNotEmpty(data)) {
				JSONObject dataObj = new JSONObject(data);
				String rdpId = ChangeUtil.getJSONString(dataObj, "rdpId");
				String bomOwner = ChangeUtil.getJSONString(dataObj, "bomOwner");
				String subjectChange= ChangeUtil.getJSONString(dataObj,"subjectChange");
				if(StringUtils.isNotEmpty(rdpId) && StringUtils.isNotEmpty(bomOwner)) {
					WTUser user = UserUtil.getUserByName(bomOwner);
					if(null != user) {
						docAndPartProcess(rdpId);
						bomProcess(rdpId, user,subjectChange);
						result = ChangeUtil.resSuccess("执行成功");
					} else {
						result = ChangeUtil.resFailed("用户名" + bomOwner + "在Windchill系统不存在");
					}
				} else {
					result = ChangeUtil.resFailed("参数rdpId、bomOwner不能为空");
				}
			} else {
				result = ChangeUtil.resFailed("数据传输不能为空");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			result = ChangeUtil.resFailed(e.getLocalizedMessage());
		} catch (WTException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 图文档流程
	 * @param rdpId
	 * @author xucr
	 * @CreateDate 2020年12月11日
	 */
	private void docAndPartProcess(String rdpId) {
		if(StringUtils.isNotEmpty(rdpId)) {
			try {
				//params.put("module", ChangeConstants.RDP_MODULE[1]); //不要模块，查询出图文档、物料变更的记录
				QuerySpec qs = QueryUtil.queryRDPChange(rdpId, null);
				logger.info("docAndPartProcess qs:" + qs.toString());
				QueryResult qr = PersistenceHelper.manager.find(qs);
				WTPrincipalReference principal = null;
				while(qr.hasMoreElements()) {
					Map<String, Object> maps = new HashMap<String, Object>();
					maps.put("rdpId", rdpId);
					RDPChange change = (RDPChange) qr.nextElement();
					Persistable per = change.getPersistable().getObject();
					String module = change.getModule();
					maps.put("module", module);
					//处理
					if(per instanceof WTDocument) {
						maps.put(OLDDOC, (WTDocument) per);
						WTDocument doc = (WTDocument) per;
						WTUser user = UserUtil.getUserByName(TMUtil.conversion(change.getOwner()));
						if(null != user) {
							principal = WTPrincipalReference.newWTPrincipalReference(user);
						} else {//在保存数据时，会校验用户是否存在但为了保守起见，这里再获取一次文档的创建者
							principal = doc.getCreator();
						}
						String softType = ChangeUtil.getSoftTypeName(doc, false);
						LifeCycleState lf = new LifeCycleState();
						doc = (WTDocument) VersionControlHelper.service.newVersion(doc);
						VersionControlHelper.setNote(doc, "通过RDP系统驱动文档修订升版。");
						//String version = ((wt.enterprise.RevisionControlled) doc).getVersionIdentifier().getValue();
						if(softType.indexOf(ChangeConstants.DOCTYPES[0]) > -1 
								&& ChangeConstants.RDP_MODULE[1].equals(module)) {//工艺文件， RDP模块是保存文档
							lf.setState(State.toState("UNDERREVIEW")); //如果是【提交审阅    SubmitReview】，自发【文档签审流程】流程
							doc.setState(lf);
							doc = (WTDocument) PersistenceHelper.manager.save(doc);
							
							//文档签审流程_0000001001, 工艺文件2, A.1$工艺文件发布流程
							String processName = "文档签审流程_" + doc.getNumber() + "," + doc.getName() + " 工艺文件发布";
							LifeCycleTemplate lifeCycle = (LifeCycleTemplate) doc.getLifeCycleTemplate().getObject();
							ProcessUtil.createProcess("文档签审流程(RDP)", 
									lifeCycle, doc, processName, principal, doc.getContainerReference(), maps);
							
						} else if(softType.indexOf(ChangeConstants.DOCTYPES[1]) > -1
								&& ChangeConstants.RDP_MODULE[1].equals(module)) {//图档类文档， RDP模块是保存文档
							lf.setState(State.toState("UNDERREVIEW"));// 如果是【正在工作 INWORK】，自发【图纸发布流程】流程
							doc.setState(lf);
							doc = (WTDocument) PersistenceHelper.manager.save(doc);
							
							//图纸发布流程_0000000983, 受控PDF图纸2, A.1
							String processName = "图纸发布流程_" + doc.getNumber() + "," + doc.getName();
							LifeCycleTemplate lifeCycle = (LifeCycleTemplate) doc.getLifeCycleTemplate().getObject();
							ProcessUtil.createProcess("图纸发布流程(RDP)", 
									lifeCycle, doc, processName, principal, doc.getContainerReference(), maps);
						}
					} else if(per instanceof WTPart) {//物料修改流程
						WTPart part = (WTPart) per;
//							//物料修改流程 - 0000000132, 成品1, A.6 (A10)-成品1
////							String processName = "物料修改流程 - " + part.getNumber() + ", " + part.getName();
////							LifeCycleTemplate lifeCycle = (LifeCycleTemplate) part.getLifeCycleTemplate().getObject();
////							ProcessUtil.createProcess("物料修改流程(RDP)", 
////									lifeCycle, part, processName, part.getCreator(), part.getContainerReference(),maps);
//							
						//版本进阶物料编码申请流程(RDP)
						WTPart newPart = ChangeConfig.createNewPart(part);

						if(null != newPart) {
							/**
							 * 先检出，将要修改的值更新到newPart中，在检入
							 * 1、原料号进阶流程中，0.0版本不能发起变更
							 * 2、原料号进阶流程中，如果属性需要更改，在提交进阶流程时，需要手动检出编辑
							 * 3、因此，结合实际情况，RDP变更提交的该流程，需要自动完成这个动作
							 * */
							String changeData = ChangeUtil.blob2str(change.getChangeInfo());
							newPart = setChangePart(newPart, changeData);
							maps.put(OLDPART, part);//将历史物料放入变量，因为数据库中存的是进阶前的物料信息，因此后续需要这个变量做逻辑处理
							
							String processName = "版本进阶物料编码申请流程_" + newPart.getNumber() + ", " + newPart.getName();
							LifeCycleTemplate lifeCycle = (LifeCycleTemplate) newPart.getLifeCycleTemplate().getObject();
							ProcessUtil.createProcess("版本进阶物料编码申请流程(RDP)", 
									lifeCycle, newPart, processName, newPart.getCreator(), newPart.getContainerReference(),maps);
						}
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			} catch (WTPropertyVetoException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 设置料号进阶编辑的属性
	 * @param part
	 * @param changeData
	 * @return
	 * @throws NotAuthorizedException
	 * @throws WTContainerException
	 * @throws WTException
	 * @throws JSONException
	 * @throws WTPropertyVetoException
	 * @author xucr
	 * @CreateDate 2021年1月29日
	 */
	private WTPart setChangePart(WTPart part, String changeData) throws NotAuthorizedException, WTContainerException, WTException, JSONException, WTPropertyVetoException {
		List<String> ibaNames = Arrays.asList(ChangeConstants.IBANAMES); //这些iba属性key与全局属性名称不一致
		if(null != part && StringUtils.isNotEmpty(changeData)) {
			TypeDefinitionReadView tv = TypeDefinitionServiceHelper.service.getTypeDefView(ChangeUtil.getSoftTypeName(part));
			JSONObject obj = new JSONObject(changeData);
			Iterator iter = obj.keys();
			Hashtable<String, Object> table = new Hashtable<String, Object>();
			part = (WTPart) GenericUtil.checkout(part);
			while(iter.hasNext()) {
				String key = (String) iter.next();
				String value = ChangeUtil.getJSONString(obj, key);
				
				AttributeDefDefaultView adv = IBAUtil.getAttributeDefinition(key); //得到IBA属性定义
				AttributeDefinitionReadView adr = tv.getAttributeByName(key);//对象属性
				if(ibaNames.contains(key)) {//特别处理，
					table.put(key, value);
				} else if(null != adv) { //IBA属性
					table.put(key, value);
				} else if(null != adr) {//对象本身属性，如：名称、编号等，前提是能编辑的
					
				}
			}
			if(table.size() > 0) {
				LWCUtil.setValue(part, table);
				//IBAUtil.forceSetIBAValues(part, table);
			}
			part = (WTPart) GenericUtil.checkin(part);
		}
		return part;
	}
	
	/**
	 * BOM变更流程
	 * @param rdpId
	 * @param models
	 * @author xucr
	 * @CreateDate 2020年12月11日
	 */
	private void bomProcess(String rdpId, WTUser user,String subjectChange) {
		if(StringUtils.isNotEmpty(rdpId)) {
			JSONObject params = new JSONObject();
			QuerySpec qs = QueryUtil.queryRDPBOMModel(rdpId, params);
			logger.info("bomProcess>>>qs:" + qs.toString());
			logger.info("bomProcess>>>rdpId:" +rdpId+"subjectChange:"+subjectChange+"user:"+user);
			try {
				QueryResult qr = PersistenceHelper.manager.find(qs);
				if(qr.size() > 0) {
					//创建eco
					WTContainer container = MaterialsApplicationService.getContainerByName("变更库");
					//在生命周期中，定义指定状态自启流程
					WTChangeOrder2 eco = createECN(container, ChangeConstants.BOM_ECO_WTCHANGEORDER2, subjectChange, user);
					TMECOITEMModel model = null;
					
					Map<String, Object> maps = new HashMap<String, Object>();
					maps.put("rdpId", rdpId);
					
					WTHashSet set = new WTHashSet();
					while(qr.hasMoreElements()) {
						RDPBOMModel rdpmodel = (RDPBOMModel) qr.nextElement();
						model = TMECOITEMModel.newTMECOITEMModel();
						seteco(rdpmodel, model, eco);
						set.add(model);
					}
					if(set.size() > 0) {
						PersistenceHelper.manager.save(set);
					}
					
					LifeCycleTemplate lifeCycle = (LifeCycleTemplate) eco.getLifeCycleTemplate().getObject();
					String processName = "BOM组ECO变更流程_" + eco.getNumber() + "-" + eco.getName();
					ProcessUtil.createProcess("BOM组ECO变更流程(RDP)", 
							lifeCycle, eco, processName, eco.getOwnership().getOwner(), eco.getContainerReference(),maps);
				}
			} catch (WTException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 创建更改通告
	 * @param container
	 * @param softType
	 * @param name
	 * @return
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年12月11日
	 */
	private WTChangeOrder2 createECN(WTContainer container, String softType, String name, WTUser user) throws Exception {
		String number = DBUtil.getNextValue("WTCHANGEORDERID_seq", 5);
//		String number = PersistenceHelper.manager.getNextSequence("WTCHANGEORDERID_seq");
		Calendar calendar = Calendar.getInstance();// 获得系统当前日期
		int year = calendar.get(Calendar.YEAR);
		int moth = calendar.get(Calendar.MONTH) + 1;
		
		WTChangeOrder2 newECN = WTChangeOrder2.newWTChangeOrder2();
		newECN.setNumber("DCN" + year + "" + moth + number); //按照原逻辑，设置编号
		newECN.setName(name);
		newECN.setChangeNoticeComplexity(ChangeNoticeComplexity.BASIC);
		
		//设置类型
		TypeDefinitionReference typeDefinitionRef = TypedUtility.getTypeDefinitionReference(softType);
		newECN.setTypeDefinitionReference(typeDefinitionRef);
		
		//设置文件夹
		String path = "/Default/RDP";
		Folder folder2=GenericUtil.getFolder(path, container);
		FolderHelper.assignLocation((FolderEntry)newECN, folder2);
		
		//设置初始状态(如果是需要启动流程，则无需设置)
		LifeCycleState lf = new LifeCycleState();
		lf.setState(State.toState("ASSIGNED"));
		newECN.setState(lf);
		
		//设置owner
		newECN.setOwnership(Ownership.newOwnership(user));
		
		newECN.setContainer(container);
		newECN = (WTChangeOrder2) PersistenceHelper.manager.save(newECN);

		//创建一个eca,与ecn同名,编码默认规则产生,用于处理问题:BOM流程,发布后端没有数据(从eca中获取数据)
		WTChangeActivity2 ca = wt.change2.WTChangeActivity2.newWTChangeActivity2(name);
		ca.setOrganization(newECN.getOrganization());
		ca.setContainer(newECN.getContainer());
		ChangeHelper2.service.saveChangeActivity(newECN, ca);

		return newECN;
	}
	
	/**
	 * 查询rdp对应windchill各种流程的状况
	 * @param data
	 * @return
	 * @author xucr
	 * @CreateDate 2020年12月28日
	 */
	public JSONObject findProcessStatus(String data) {
		JSONObject result = null;
		if(StringUtils.isNotEmpty(data)) {
			try {
				JSONObject dataObj = new JSONObject(data);
				String rdpId = ChangeUtil.getJSONString(dataObj, "rdpId");
				if(StringUtils.isNotEmpty(rdpId)) {
//					result = QueryUtil.findProcessStatus(rdpId, FAILED); //查询失败的流程
					result = QueryUtil.findProcessStatus(rdpId, ""); //查询所有的流程
				} else {
					result = ChangeUtil.resFailed("参数rdpId不能为空");
				}
			} catch (JSONException e) {
				e.printStackTrace();
				result = ChangeUtil.resFailed(e.getLocalizedMessage());
			}
		} else {
			result = ChangeUtil.resFailed("数据不能为空");
		}
		return result;
	}
	
	/**
	 * 获取直接子项
	 * @param data
	 * @return
	 * @author xucr
	 * @CreateDate 2021年1月27日
	 */
	public JSONObject getChildren(String data) {
		JSONObject result = null;
		if(StringUtils.isNotEmpty(data)) {
			try {
				JSONObject dataObj = new JSONObject(data);
				String partNum = ChangeUtil.getJSONString(dataObj,"partNum");
				String view = ChangeUtil.getJSONString(dataObj,"view");
				if(StringUtils.isNotEmpty(partNum) && StringUtils.isNotEmpty(view)) {
					WTPart part = CoreUtil.getWTPartByNumberAndView(partNum, view);
					JSONObject _dataObj = ChangeUtil.getChildren(part); //一定会有返回值，即使没有父项，也会按照结构返回
					result = new JSONObject();
					result.put(ChangeUtil.CODE, ChangeUtil.CODE_SUCCESS);
			    	result.put(ChangeUtil.SUCCESS, true);
			    	result.put(ChangeUtil.COUNT, _dataObj.length());
			    	result.put(ChangeUtil.DATA, _dataObj);
					result.put(ChangeUtil.MSG, "");
				} else {
					result = ChangeUtil.resFailed("参数partNum、view不能为空");
				}
			} catch (JSONException e) {
				e.printStackTrace();
				result = ChangeUtil.resFailed(e.getLocalizedMessage());
			} catch (WTException e) {
				e.printStackTrace();
				result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
			} catch (WTPropertyVetoException e) {
				e.printStackTrace();
				result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
			}
		} else {
			result = ChangeUtil.resFailed("数据不能为空");
		}
		return result;
	}
	
	/**
	 * 获取特定替代料
	 * @param data
	 * @return
	 * @author xucr
	 * @CreateDate 2021年1月27日
	 */
	public JSONObject getSubstitutes(String data) {
		JSONObject result = null;
		if(StringUtils.isNotEmpty(data)) {
			try {
				JSONObject dataObj = new JSONObject(data);
				String partNum = ChangeUtil.getJSONString(dataObj,"partNum");//BOM
				String view = ChangeUtil.getJSONString(dataObj,"view");//视图
				String mainNum = ChangeUtil.getJSONString(dataObj,"mainNum");//主料
				
				if(StringUtils.isNotEmpty(partNum) && StringUtils.isNotEmpty(mainNum) && StringUtils.isNotEmpty(view)) {
					WTPart bomPart = CoreUtil.getWTPartByNumberAndView(partNum, view);
					WTPart mainPart = CoreUtil.getWTPartByNumberAndView(mainNum, view);
					
					JSONArray array = ChangeUtil.getSubstitutes(bomPart, mainPart);
					result = ChangeUtil.resSuccess(array, array.length());
				} else {
					result = ChangeUtil.resFailed("参数partNum、mainNum、view不能为空");
				}
			} catch (JSONException e) {
				e.printStackTrace();
				result = ChangeUtil.resFailed(e.getLocalizedMessage());
			} catch (WTException e) {
				e.printStackTrace();
				result = ChangeUtil.resFailed(e.getLocalizedMessage(Locale.getDefault()));
			}
		} else {
			result = ChangeUtil.resFailed("数据不能为空");
		}
		return result;
	}
}
 